package com.mfhcd.facepay.activity

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import android.text.TextUtils
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem
import com.githang.statusbar.StatusBarCompat
import com.jakewharton.rxbinding3.view.clicks
import com.mfhcd.facepay.R
import com.mfhcd.facepay.base.BaseActivity
import com.mfhcd.facepay.constant.ArouterUrl
import com.mfhcd.facepay.databinding.ActivityMainBinding
import com.mfhcd.facepay.fragment.IndexFragment
import com.mfhcd.facepay.fragment.MineFragment
import com.mfhcd.facepay.fragment.QueryFragment
import com.mfhcd.facepay.model.ResponseModel
import com.mfhcd.facepay.utils.Constants
import com.mfhcd.facepay.utils.PhotoUtil
import com.mfhcd.facepay.utils.ToastUtil
import com.mfhcd.facepay.viewmodel.TestViewModel
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.collections.ArrayList

@Route(path = ArouterUrl.ACTIVITY_MAIN)
class MainActivity : BaseActivity<TestViewModel, ActivityMainBinding>() {
    private lateinit var fragments: ArrayList<Fragment>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        hideTitle()
    }

    override fun initData() {
        initNavigateBar()
    }



    private fun initNavigateBar() {
        bindingView.navigateMain.setMode(BottomNavigationBar.MODE_FIXED)
        bindingView.navigateMain.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
        bindingView.navigateMain.addItem(BottomNavigationItem(R.drawable.tab_home_selector, "首页").setActiveColorResource(R.color.colorPrimary))
                .addItem(BottomNavigationItem(R.drawable.tab_query_selector, "查询").setActiveColorResource(R.color.colorPrimary))
                .addItem(BottomNavigationItem(R.drawable.tab_mine_selector, "我的").setActiveColorResource(R.color.colorPrimary))
                .setFirstSelectedPosition(0)
                .initialise()
        fragments = getFragments()
        setDefaultFragment()
    }

    private fun getFragments(): ArrayList<Fragment> {
        fragments = ArrayList()
        fragments.add(IndexFragment.newInstance())
        fragments.add(QueryFragment.newInstance())
        fragments.add(MineFragment.newInstance())
        return fragments
    }

    /**
     * 设置默认的
     */
    private fun setDefaultFragment() {
        val fm = supportFragmentManager
        val transaction = fm.beginTransaction()
        transaction.replace(R.id.fl_container_main, fragments[0])
        transaction.commit()
    }

    @SuppressLint("CheckResult")
    override fun initListener() {

        bindingView.navigateMain.setTabSelectedListener(object : BottomNavigationBar.OnTabSelectedListener {
            override fun onTabReselected(position: Int) {

            }

            override fun onTabUnselected(position: Int) {
                if (position < fragments.size) {
                    val fm: FragmentManager = supportFragmentManager
                    val ft: FragmentTransaction = fm.beginTransaction()
                    val fragment: Fragment = fragments[position]
                    ft.hide(fragment)
                    ft.commitAllowingStateLoss()
                }
            }

            override fun onTabSelected(position: Int) {
                if (fragments != null) {
                    if (position < fragments.size) {
                        val fm = supportFragmentManager
                        val ft = fm.beginTransaction()
                        val fragment = fragments[position]
                        if (fragment.isAdded) {
                            ft.show(fragment)
                        } else {
                            ft.add(R.id.fl_container_main, fragment)
                        }
                        ft.commitAllowingStateLoss()
                        when (position) {
                            0 -> StatusBarCompat.setStatusBarColor(Objects.requireNonNull(mContext as Activity), ContextCompat.getColor(mContext, R.color.white), true)
                            1 -> StatusBarCompat.setStatusBarColor(Objects.requireNonNull(mContext as Activity), ContextCompat.getColor(mContext, R.color.blue), true)
                            2 -> StatusBarCompat.setStatusBarColor(Objects.requireNonNull(mContext as Activity), ContextCompat.getColor(mContext, R.color.blue), true)
                            else -> {
                            }
                        }
                    }
                }

            }
        })

    }
}
